package com.sc1905.crane.reading.controller;


import com.github.pagehelper.PageInfo;
import com.sc1905.crane.pojo.NBookshelf;
import com.sc1905.crane.pojo.NBrowse;
import com.sc1905.crane.pojo.NChapter;
import com.sc1905.crane.pojo.NNovel;
import com.sc1905.crane.reading.service.BookShelfService;
import com.sc1905.crane.reading.service.BrowseService;
import com.sc1905.crane.reading.service.ChapterService;
import com.sc1905.crane.reading.service.NovelService;
import com.sc1905.crane.reading.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/novel")
public class NovelController {

    @Resource
    NovelService novelService;
    @Resource
    ChapterService chapterService;
    @Resource
    BrowseService browseService;
    @Resource
    BookShelfService bookShelfService;

    //小说详情
    @RequestMapping("/novelDetails")
    public String novelDetails(
            Integer novelId,
            Model model,
            @RequestParam(defaultValue = "1")Integer pageNum,
            @RequestParam(defaultValue = "10")Integer pageSize,
            String b
    ){
        if (b==null){
            NBookshelf haveHistory = bookShelfService.isHaveHistory(novelId);
            if (haveHistory!=null){
                b="1";
            }
        }
        //写死userId***************************************
        NBrowse read = browseService.isRead(1, novelId);
        if (read==null){//根据是否有浏览记录 判断是开始阅读 || 继续阅读
            model.addAttribute("status",0);
        }else {
            model.addAttribute("status",1);
        }
        //写死userId***************************************
        List<NNovel> nNovels = novelService.otherWorks(1, novelId);//作者其它作品
        NNovel nNovel = novelService.novelDetails(novelId);//小说详情
        List<NChapter> nChapters = chapterService.novelChapter(novelId);//小说章节集合
        PageInfo<NChapter> nChapterPageInfo = chapterService.chapterPage(novelId, pageNum, pageSize);
        String i = chapterService.chapterPrice(novelId);//小说单章价格
        String typeName = novelService.novelType(nNovel.getTypeid());//小说类型
        String count = "";
        for (NChapter a : nChapters){
            count += a.getChaptername()+a.getContent();
        }
        int count1 = Util.count(count);
        model.addAttribute("novel",nNovel);
        model.addAttribute("chapters",nChapters);
        model.addAttribute("count",count1);
        model.addAttribute("price",i);
        model.addAttribute("page",nChapterPageInfo);
        model.addAttribute("typeName",typeName);
        model.addAttribute("otherWorks",nNovels);
        model.addAttribute("a",b);
        return "reading/details";
    }

    //小说内容展示
    @RequestMapping("/chapterContent")
    public String chapterContent(
            Integer chapterId,
            Integer novelId,
            Model model,
            @RequestParam(defaultValue = "1")Integer pageNum,
            @RequestParam(defaultValue = "1")Integer pageSize){
        PageInfo<NChapter> nChapterPageInfo = chapterService.chapterPage(novelId, chapterId, pageSize);
        model.addAttribute("page",nChapterPageInfo);
        NChapter nChapter = chapterService.chapterContent(chapterId);
        model.addAttribute("chapterContent",nChapter);
        return "reading/read";
    }

    //点击阅读
    @RequestMapping("/startRead")
    public String startRead(
            String chapterId,
            Integer novelId,
            Model model,
            String sta,
            String b){
        if (b==null){
            NBookshelf haveHistory = bookShelfService.isHaveHistory(novelId);
            if (haveHistory!=null){
                b="1";
            }
        }
        model.addAttribute("a",b);
        model.addAttribute("novelId",novelId);
        NChapter nChapter = chapterService.firstRead();
        //写死userId***************************************
        NBrowse read = browseService.isRead(1, novelId);
        if (read == null){//当当前用户的这本小说浏览记录不存在时 插入浏览记录
            if (chapterId==null){
                NBrowse nBrowse = new NBrowse();
                nBrowse.setBookid(novelId);
                nBrowse.setChapterid(nChapter.getChapterid());
                nBrowse.setTime(new Date());
                nBrowse.setUserid(1);//写死userId****************************************
                browseService.insertBrowse(nBrowse);
                model.addAttribute("chapterContent",nChapter);
                if ("1".equals(sta)){
                    return "reading/allRead";
                }else {
                    return "reading/read";
                }
            }else {
                NBrowse nBrowse = new  NBrowse();
                nBrowse.setBookid(novelId);
                nBrowse.setChapterid(Integer.parseInt(chapterId));
                nBrowse.setTime(new Date());
                nBrowse.setUserid(1);//写死userId****************************************
                browseService.insertBrowse(nBrowse);
                NChapter Chapter = chapterService.chapterContent(Integer.parseInt(chapterId));
                model.addAttribute("chapterContent",Chapter);
//                if ("1".equals(sta)){
//                    return "reading/allRead";
//                }else {
                    return "reading/read";
//                }
            }

        }else { //当浏览记录存在更新该浏览记录

            read.setTime(new Date());
            if (chapterId!=null){
                read.setChapterid(Integer.parseInt(chapterId));
            }
            browseService.updateBrowse(read);
            NChapter Chapter = chapterService.chapterContent(read.getChapterid());
            model.addAttribute("chapterContent",Chapter);
            if ("1".equals(sta)){
                return "reading/allRead";
            }else {
                return "reading/read";
            }
        }
    }

    //全目录展示
    @RequestMapping("/allList")
    public String allList(Integer novelId,Model model,String b){
        if (b==null){
            NBookshelf haveHistory = bookShelfService.isHaveHistory(novelId);
            if (haveHistory!=null){
                b="1";
            }
        }
        model.addAttribute("a",b);
        List<NChapter> nChapters = chapterService.novelChapter(novelId);//小说章节集合
        NNovel nNovel = novelService.novelDetails(novelId);//小说详情
        model.addAttribute("chapters",nChapters);
        model.addAttribute("novelId",novelId);
        model.addAttribute("novel",nNovel);
        return "reading/list";
    }

    //加入书架
    @Transactional
    @RequestMapping("/addBookShelf")
    public String addBookShelf(Integer novelId,String c,String chapterId){
        NBookshelf haveHistory = bookShelfService.isHaveHistory(novelId);
        String b = "0";
        if (haveHistory==null){
            NBookshelf bookshelf = new NBookshelf();
            //写死userId***********************************************
            bookshelf.setUserid(1);
            bookshelf.setBookid(novelId);
            bookShelfService.AddBookShelf(bookshelf);
            b = "1";
        }else {
            //写死userId**************************************
            bookShelfService.deleteBookShelf(1,novelId);
            b = "0";
        }
        if (c.equals("1")){//为小说页详情时 details
            return "forward:novelDetails?novelId="+novelId+"&b="+b;
        }else if (c.equals("2")){//为详情页点击进入阅读页时 read页面
            return "redirect:startRead?novelId="+novelId+"&b="+b+"&chapterId="+chapterId;
        }else if (c.equals("3")){//详情页进入目录页面时 list
            return "forward:allList?novelId="+novelId+"&b="+b;
        }else {//全部目录进入阅读页时allRead
            return "redirect:startRead?novelId="+novelId+"&b="+b+"&chapterId="+chapterId+"&sta=1";
        }

    }

    @RequestMapping("/topPage")
    public String topPage(Integer chapterId,Integer novelId){
        List<NChapter> nChapters = chapterService.novelChapter(novelId);
        String topId="0";
        for (int i=0;i<nChapters.size();i++){
            System.out.println(nChapters.get(i));
            if (chapterId.equals(nChapters.get(i).getChapterid())){
                if (i-1<0){
                    topId=nChapters.get(i).getChapterid().toString();
                }else {
                    topId=nChapters.get(i-1).getChapterid().toString();
                }
            }
        }
        return "redirect:startRead?chapterId="+topId+"&novelId="+novelId;
    }

    @RequestMapping("/lastPage")
    public String lastPage(Integer chapterId,Integer novelId){
        List<NChapter> nChapters = chapterService.novelChapter(novelId);
        String topId="0";
        for (int i=0;i<nChapters.size();i++){
            if (chapterId.equals(nChapters.get(i).getChapterid())){
                if (i+1>nChapters.size()-1){
                    topId=nChapters.get(i).getChapterid().toString();
                }else {
                    topId=nChapters.get(i+1).getChapterid().toString();
                }
            }
        }
        return "redirect:startRead?chapterId="+topId+"&novelId="+novelId;
    }


}
